using OpenAI.ObjectModels;
using Newtonsoft.Json;



#if BACKEND
using MultiAgentsServer.Scripts.Database;
#endif

namespace MultiAgentsClient.Shared;

public class LlmMessageSimp
{
    //TODO: this should be string drop down, not enum
    public enum LlmMessageType
    {
        system,
        user,
        assistant,
        tool
    }

    [JsonIgnore] public LlmMessageType MessageType { get; set; }

    //this is for serialization only
    public string MessageFrom { get {

            switch (MessageType)
            {
                case LlmMessageType.system:
                    return "System";
                case LlmMessageType.user:
                    return "User";
                case LlmMessageType.assistant:
                    return "LLM";
                case LlmMessageType.tool:
                    return "ToolCall";
                default:
                    return "UnSupported";
            }
        }
    }

    public string Message { get; set; } = "";

    public LlmMessageSimp(string role, string message)
    {
        if (role == StaticValues.ChatMessageRoles.System)
        {
            MessageType = LlmMessageType.system;
        }
        else if (role == StaticValues.ChatMessageRoles.User)
        {
            MessageType = LlmMessageType.user;
        }
        else if (role == StaticValues.ChatMessageRoles.Tool)
        {
            MessageType = LlmMessageType.tool;
        }
        else if (role == StaticValues.ChatMessageRoles.Assistant)
        {
            MessageType = LlmMessageType.assistant;
        }
        else
        {
            throw new Exception($"Not supported role {role}");
        }

        Message = message;
    }

    public LlmMessageSimp()
    {
    }

    public string ToString()
    {
        string outputStr = "";
        switch (MessageType)
        {
            case LlmMessageType.system:
                outputStr += $"System Message:\n {Message}\n\n\n\n";
                break;
            case LlmMessageType.user:
                outputStr += $"User Message:\n {Message}\n\n\n\n";
                break;
            case LlmMessageType.assistant:
                outputStr += $"Assistant Message:\n {Message}\n\n\n\n";
                break;
            case LlmMessageType.tool:
                outputStr += $"Tool Message:\n {Message}\n\n\n\n";
                break;
        }
        return outputStr;
    }

#if BACKEND
    public static LlmMessageSimp FromMessageDB(MessageDB chatMsgDb)
    {
        return new LlmMessageSimp(chatMsgDb.Role, chatMsgDb.Content);
    }
#endif
}
